package com.hxzy.wx.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.hxzy.common.exception.HxdsException;
import com.hxzy.common.util.R;
import com.hxzy.common.util.TencentCloudUtil;
import com.hxzy.wx.controller.req.DeleteCosFileReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/12/15 17:06
 * @Version 1.0
 */
@RestController
@RequestMapping("/cos")
@Log4j2
@Tag(name = "WxTencentUploadController", description = "对象存储Web接口")
public class WxTencentUploadController {

    /**
     * 腾讯云文件上传工具类
     */
    @Autowired
    private TencentCloudUtil cosUtil;


    @PostMapping("/uploadCosPublicFile")
    @SaCheckLogin
    @Operation(summary = "上传文件到公有桶")
    public R uploadCosPublicFile(@Param("file") MultipartFile file, @Param("module") String module) {
        if (file.isEmpty()) {
            throw new HxdsException("上传文件不能为空");
        }
        try {
            HashMap map = cosUtil.uploadPublicFile(file, "");
            return R.ok(map);
        } catch (IOException e) {
            log.error("文件上传到腾讯云错误", e);
            throw new HxdsException("文件上传到腾讯云错误");
        }
    }

    @PostMapping("/uploadCosPrivateFile")
    @SaCheckLogin
    @Operation(summary = "上传文件到私有桶")
    public R uploadCosPrivateFile(@Param("file") MultipartFile file, @Param("module") String module) {
        if (file.isEmpty()) {
            throw new HxdsException("上传文件不能为空");
        }
        try {
            String path = "";
            //司机认证的时候图片上传
            if ("driverAuth".equals(module)) {
                path = "/driver/auth";
            } else {
                throw new HxdsException("module错误");
            }
            HashMap map = cosUtil.uploadPrivateFile(file, path);
            return R.ok(map);
        } catch (IOException e) {
            log.error("文件上传到腾讯云错误", e);
            throw new HxdsException("文件上传到腾讯云错误");
        }
    }

    @PostMapping("/deleteCosPublicFile")
    @SaCheckLogin
    @Operation(summary = "删除公有桶文件")
    public R deleteCosPublicFile(@Valid @RequestBody DeleteCosFileReq form) {
        cosUtil.deletePublicFile(form.getPathes());
        return R.ok();
    }

    @PostMapping("/deleteCosPrivateFile")
    @SaCheckLogin
    @Operation(summary = "删除私有桶文件")
    public R deleteCosPrivateFile(@Valid @RequestBody DeleteCosFileReq form) {
        cosUtil.deletePrivateFile(form.getPathes());
        return R.ok();
    }


}
